package team.you.ticket.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.you.ticket.dao.CarMapper;
import team.you.ticket.pojo.Car;
import team.you.ticket.pojo.Result;
import team.you.ticket.service.CarService;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;

    @Override
    public Result getAllCars(Integer page, Integer limit) {
        Result result = new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        List<Car> cars = carMapper.selectAll(page, limit);
        Integer count = carMapper.getCount();
        result.setData(cars);
        result.setCount(count);
        return result;
    }

    @Override
    public Result add(Car car) {
        Result result = new Result();
        String carNum = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";
        String id = UUID.randomUUID().toString();
        car.setId(id);
        try {
            Car car1 = carMapper.selectByCarNum(car.getCarNum());
            if (isCarNum(carNum,car.getCarNum()) && car1 == null) {
                carMapper.insert(car);
                result.setMsg("新增成功");
                return result;
            }
            else
            {
                result.setCode(1);
                result.setMsg("车牌号重复或违法");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result del(String id) {
        Result result = new Result();
        Car car = carMapper.selectByPrimaryKey(id);
        if (car != null) {
            carMapper.deleteByPrimaryKey(id);
            result.setMsg("删除成功");
        } else {
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }

    @Override
    @Transactional //添加事务
    //事务 做事时，要么全部成功，要么全部失败 如果出现异常，必须全部回滚
    public Result delete(String[] ids) {
        Result result = new Result();
        try {
            for (int i = 0; i < ids.length; i++) {
                carMapper.deleteByPrimaryKey(ids[i]);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result update(String id, String value, String field) {
        Result result=new Result();
        try {
            Integer i=carMapper.updateByKey(id,value,field);
            if (i==1)
            {
                result.setMsg("更新成功");
                return result;
            }
            else
            {
                result.setCode(500);
                result.setMsg("更新失败");
                return result;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result selectByLike(String value, String type, Integer page, Integer limit) {
        Result result = new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        Integer type0=Integer.parseInt(type);
        List<Car> cars = carMapper.selectByLike(value,type0,page, limit);
        List<Car> count = carMapper.selectByLike(value,type0,null,null);
        result.setData(cars);
        result.setCount(count.size());
        return result;
    }

    /**
     * 验证车牌号是否符合规则
     *
     * @param car_num 输入的车牌号
     * @return 验证结果，验证通过返回true，失败返回false
     */
    public static boolean isCarNum(String carNum, String car_num) {
        Pattern p1 = Pattern.compile(carNum);
        Matcher m1 = p1.matcher(car_num);
        boolean bool = m1.matches();
        return bool;
    }
}